package cn.edu.flow.test;

import cn.edu.flow.util.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.activiti.api.model.shared.model.VariableInstance;
import org.activiti.api.process.model.ProcessInstance;
import org.activiti.api.process.model.builders.ProcessPayloadBuilder;
import org.activiti.api.process.runtime.ProcessRuntime;
import org.activiti.api.runtime.shared.query.Page;
import org.activiti.api.runtime.shared.query.Pageable;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

/**
 * activiti7 新特性之：ProcessRuntime
 */
@Slf4j
@SpringBootTest
public class Part8_ProcessRuntime {

    @Autowired
    private ProcessRuntime processRuntime;

    @Autowired
    private SecurityUtil securityUtil;

    /**
     * 获取流程实例
     */
    @Test
    public void getProcessInstance() {
        securityUtil.logInAs("bajie");
        Page<ProcessInstance> processInstancePage = processRuntime
                .processInstances(Pageable.of(0,100));
        log.info("流程实例数量："+processInstancePage.getTotalItems());
        List<ProcessInstance> list = processInstancePage.getContent();
        for(ProcessInstance pi : list){
            log.info("-----------------------");
            log.info("getId：" + pi.getId());
            log.info("getName：" + pi.getName());
            log.info("getStartDate：" + pi.getStartDate());
            log.info("getStatus：" + pi.getStatus());
            log.info("getProcessDefinitionId：" + pi.getProcessDefinitionId());
            log.info("getProcessDefinitionKey：" + pi.getProcessDefinitionKey());

        }
    }

    /**
     * 启动流程实例
     */
    @Test
    public void startProcessInstance() {
        securityUtil.logInAs("bajie");
        ProcessInstance processInstance = processRuntime.start(ProcessPayloadBuilder
                .start()
                .withProcessDefinitionKey("myProcess_ProcessRuntime")
                .withName("第一个流程实例名称")
                //.withVariable("","")
                .withBusinessKey("自定义bKey")
                .build()
        );
    }

    /**
     * 删除流程实例
     */
    @Test
    public void delProcessInstance() {
        securityUtil.logInAs("bajie");
        ProcessInstance processInstance = processRuntime.delete(ProcessPayloadBuilder
                .delete()
                .withProcessInstanceId("6fcecbdb-d3e0-11ea-a6c9-dcfb4875e032")
                .build()
        );
    }

    /**
     * 挂起流程实例
     */
    @Test
    public void suspendProcessInstance() {
        securityUtil.logInAs("bajie");
        ProcessInstance processInstance = processRuntime.suspend(ProcessPayloadBuilder
                .suspend()
                .withProcessInstanceId("1f2314cb-cefa-11ea-84aa-dcfb4875e032")
                .build()
        );
    }

    /**
     * 激活流程实例
     */
    @Test
    public void resumeProcessInstance() {
        securityUtil.logInAs("bajie");
        ProcessInstance processInstance = processRuntime.resume(ProcessPayloadBuilder
                .resume()
                .withProcessInstanceId("1f2314cb-cefa-11ea-84aa-dcfb4875e032")
                .build()
        );
    }


    /**
     * 流程实例参数
     */
    @Test
    public void getVariables() {
        securityUtil.logInAs("bajie");
        List<VariableInstance> list = processRuntime.variables(ProcessPayloadBuilder
                .variables()
                .withProcessInstanceId("2b2d3990-d3ca-11ea-ae96-dcfb4875e032")
                .build()
        );
        for(VariableInstance vi : list){
            log.info("-------------------");
            log.info("getName：" + vi.getName());
            log.info("getValue：" + vi.getValue());
            log.info("getTaskId：" + vi.getTaskId());
            log.info("getProcessInstanceId：" + vi.getProcessInstanceId());
        }
    }

}
